package com.qk.common.util;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.ListObjectsV2Request;
import com.aliyun.oss.model.ListObjectsV2Result;
import com.aliyun.oss.model.OSSObjectSummary;
import com.qk.common.config.AliYunOSSProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kaoM1Jy
 * @date 2025/10/11 19:33
 * @description:
 */
@Slf4j
@Component
public class GetOSSAllFiles {

    @Autowired
    private AliYunOSSProperties aliYunOSSProperties;

    public List<String> listAllObjects() throws Exception {
        int maxKeys = 200;
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);

        OSS ossClient = null;
        List<String> objectKeys = new ArrayList<>(); // 创建用于存储对象键的集合

        try {
            ossClient = OSSClientBuilder.create()
                    .endpoint(aliYunOSSProperties.getEndpoint())
                    .credentialsProvider(credentialsProvider)
                    .clientConfiguration(clientBuilderConfiguration)
                    .region(aliYunOSSProperties.getRegion())
                    .build();

            String nextContinuationToken = null;
            ListObjectsV2Result result;

            do {
                ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request(aliYunOSSProperties.getBucketName())
                        .withMaxKeys(maxKeys);
                if (nextContinuationToken != null) {
                    listObjectsV2Request.setContinuationToken(nextContinuationToken);
                }

                result = ossClient.listObjectsV2(listObjectsV2Request);

                List<OSSObjectSummary> sums = result.getObjectSummaries();
                for (OSSObjectSummary s : sums) {
                    objectKeys.add(s.getKey()); // 将对象键添加到集合中
                }

                nextContinuationToken = result.getNextContinuationToken();

            } while (result.isTruncated());

        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.");
            log.error("Error Message: {}", oe.getErrorMessage());
            log.error("Error Code: {}", oe.getErrorCode());
            log.error("Request ID: {}", oe.getRequestId());
            log.error("Host ID: {}", oe.getHostId());
        } catch (ClientException ce) {
            log.error("Caught a ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS.");
            log.error("Error Message: {}", ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
       objectKeys.forEach(key -> log.info("Object Key: {}", key));
        return objectKeys; // 返回包含所有对象键的集合
    }
}
